package com.ktgj.other.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.utils.SnowFlake;
import com.ktgj.common.redis.service.RedisService;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.*;
import com.ktgj.other.common.RentCar;
import com.ktgj.other.domain.*;
import com.ktgj.other.domain.common.*;
import com.ktgj.other.domain.vo.PayTypeVo;
import com.ktgj.other.domain.vo.RcOrderVO;
import com.ktgj.other.service.IRcOrderDetailService;
import com.ktgj.other.service.IRcOrderPaymentrecordService;
import com.ktgj.other.service.IRcOrderTrackingService;
import com.ktgj.pay.api.RemotePayService;
import com.ktgj.pay.api.domain.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.other.mapper.RcOrderMapper;
import com.ktgj.other.service.IRcOrderService;

import javax.servlet.http.HttpServletRequest;

/**
 * 专车订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-27
 */
@Service
public class RcOrderServiceImpl implements IRcOrderService 
{
    @Autowired
    private RcOrderMapper rcOrderMapper;
    @Autowired
    private IRcOrderDetailService rcOrderDetailService;
    @Autowired
    private RentCar rentCar;
    @Autowired
    private RemoteCustomerService remoteCustomerService;
    @Autowired
    private IRcOrderPaymentrecordService rcOrderPaymentrecordService;
    @Autowired
    private IRcOrderTrackingService rcOrderTrackingService;
    @Autowired
    private RemotePayService payService;
    @Autowired
    private RedisService redisService;

    /**
     * 查询专车订单
     * 
     * @param rcorderid 专车订单ID
     * @return 专车订单
     */
    @Override
    public RcOrder selectRcOrderById(Long rcorderid)
    {
        return rcOrderMapper.selectRcOrderById(rcorderid);
    }

    /**
     * 查询专车订单列表
     * 
     * @param rcOrder 专车订单
     * @return 专车订单
     */
    @Override
    public List<RcOrder> selectRcOrderList(RcOrder rcOrder)
    {
        return rcOrderMapper.selectRcOrderList(rcOrder);
    }

    //专车订单列表
    @Override
    public List<RcOrder> queryRcOrderList(RcOrder rcOrder) {
        if (rcOrder.getPayTimeEnd() != null && !rcOrder.getPayTimeEnd().equals(""))
            rcOrder.setPayTimeEnd(rcOrder.getPayTimeEnd() + " 23:59:59");
        return rcOrderMapper.queryRcOrderList(rcOrder);
    }

    /**
     * 新增专车订单
     * 
     * @param rcOrder 专车订单
     * @return 结果
     */
    @Override
    public int insertRcOrder(RcOrder rcOrder)
    {
        return rcOrderMapper.insertRcOrder(rcOrder);
    }

    /**
     * 修改专车订单
     * 
     * @param rcOrder 专车订单
     * @return 结果
     */
    @Override
    public int updateRcOrder(RcOrder rcOrder)
    {
        return rcOrderMapper.updateRcOrder(rcOrder);
    }

    /**
     * 批量删除专车订单
     * 
     * @param rcorderids 需要删除的专车订单ID
     * @return 结果
     */
    @Override
    public int deleteRcOrderByIds(Long[] rcorderids)
    {
        return rcOrderMapper.deleteRcOrderByIds(rcorderids);
    }

    /**
     * 删除专车订单信息
     * 
     * @param rcorderid 专车订单ID
     * @return 结果
     */
    @Override
    public int deleteRcOrderById(Long rcorderid)
    {
        return rcOrderMapper.deleteRcOrderById(rcorderid);
    }

    /**
     * 查询专车订单
     * @param orderid
     * @return
     */
    @Override
    public RcOrder queryRcOrderByOrderid(Long orderid) {
        return rcOrderMapper.queryRcOrderByOrderid(orderid);
    }

    //订单填写
    @Override
    public RcFillOrder fillOrder(String token, DayPriceRequest request) throws Exception {
        RcFillOrder order = new RcFillOrder();
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        if (customer.getCode() == 200 && customer.getData() != null) {
            order.setPassengername(customer.getData().getCustomerName());
            order.setPassengertel(customer.getData().getContractPhoneOne());
        } else {
            throw new Exception(customer.getMsg());
        }
        switch (request.serviceType) {
            case 11:
                order.setServiceType("接机");
                break;
            case 12:
                order.setServiceType("送机");
                break;
            case 21:
                order.setServiceType("接站");
                break;
            case 22:
                order.setServiceType("送站");
                break;
        }
        order.setFlightNumber(request.flightNum);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(new Date(request.useTime * 1000L));
        Date date = dateFormat.parse(format);
        order.setUseTime(date);
        order.setStartAddress(request.startAddress);
        order.setEndAddress(request.endAddress);
        DayPriceResponse dayPrice = rentCar.getDayPrice(request);
        if (dayPrice.IsSucess) {
            dayPrice.queryResultList.forEach(item -> {
                Map<String, String> map = carType((long) item.carType);
                item.brand = map.get("brand");
                item.cartypename = map.get("cartypename");
            });
            order.setPriceMark(dayPrice.priceMark);
            order.getQueryResultList().addAll(dayPrice.queryResultList);
        } else
            throw new Exception(dayPrice.msg);

        R<Long> depb = remoteCustomerService.queyUserDepositBusinessByToken(token);
        if (depb.getCode() == 200) {
            Long depositBusiness = depb.getData();
            order.setDepositBusiness(depositBusiness);
        }
        R<Long> integralVoR = remoteCustomerService.queyUserCreditsNumByToken(token);
        if (integralVoR.getCode() == 200) {
            Long points = integralVoR.getData();
            order.setPoints(points);
        }
        if (dayPrice.queryResultList.size() > 0) {
            BigDecimal seatPrice = null;
            if (request.carTypeId != 0) {
                for (productDTO productDTO : dayPrice.queryResultList) {
                    if (productDTO.carType == request.carTypeId) {
                        seatPrice = BigDecimal.valueOf(productDTO.price);
                        break;
                    }
                }
            } else {
                seatPrice = BigDecimal.valueOf(dayPrice.queryResultList.get(0).price);
            }
            R<CouponDto> couponDtoR = remoteCustomerService.queyUserCouponByToken(token, seatPrice);
            if (couponDtoR.getCode() == 200) {
                CouponDto data = couponDtoR.getData();
                order.setCoupon(data);
            }
        }
        order.setInvoice("取车时现场向代泊员索取额定发票");
        return order;
    }

    //创建订单
    @Override
    public String createOrder(String token, RcOrder rcOrder) throws Exception {
        RcOrderDetail rcOrderDetail = new RcOrderDetail();
        CalculationVOApi calculationVOApi = new CalculationVOApi();
        List<BigDecimal> unitPriceList = new ArrayList<>();
        R<Customer> customer = null;
        String orderType = "专车";
        if (token != null) {
            customer = remoteCustomerService.getLoginCustomerByToken(token);
            if (customer.getCode() == 200 && customer.getData() != null) {
                rcOrder.setJsjmemberid(String.valueOf(customer.getData().getCustomerId()));
                rcOrder.setOrdersource(1L);
            } else {
                throw new Exception(customer.getMsg());
            }
            rcOrder.setServiceamount(new BigDecimal(0));
        } else {
            rcOrder.setJsjmemberid(rcOrder.getJsjmemberid());
            rcOrder.setOrdersource(3L);
            rcOrder.setServiceamount(new BigDecimal(30));
            unitPriceList.add(new BigDecimal(30));
        }
        //金币
        BigDecimal depositSkipPwdAmount = new BigDecimal(0);
        R<Long> longR;
        if (token != null && !token.equals("")){
            longR = remoteCustomerService.queyUserDepositBusinessByToken(token);
        }
        else {
            longR = remoteCustomerService.selectAvailableGoldByCustomerId(Long.valueOf(rcOrder.getJsjmemberid()));
        }
        if (longR.getData() != null) {
            depositSkipPwdAmount = new BigDecimal(longR.getData());
        }
        //积分
        BigDecimal points;
        R<Long> pntValueR;
        if (token != null && !token.equals("")){
            pntValueR = remoteCustomerService.queyUserCreditsNumByToken(token);
        }
        else {
            pntValueR = remoteCustomerService.queyUserCreditsNumByCustomerId(Long.valueOf(rcOrder.getJsjmemberid()));
        }
        Long pntValue = pntValueR.getData();
        DayPriceRequest request = new DayPriceRequest();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String boardingTime = dateFormat.format(rcOrder.getBoardingtime());
        Date parse = dateFormat.parse(boardingTime);
        long time = parse.getTime() / 1000;
        switch (rcOrder.getUsedcarway().toString()) {
            case "1":
                request.serviceType = 11;
                break;
            case "2":
                request.serviceType = 12;
                break;
            case "3":
                request.serviceType = 21;
                break;
            case "4":
                request.serviceType = 22;
                break;
        }
        request.useTime = time;
        List<Integer> carType = new ArrayList<>();
        carType.add(Math.toIntExact(rcOrder.getCarlevel()));
        request.carTypeList = carType;
        request.startAddress = rcOrder.getGetonaddress();
        request.startLng = Double.parseDouble(rcOrder.getGetonpositionlng());
        request.startLat = Double.parseDouble(rcOrder.getGetonpositionlat());
        request.endAddress = rcOrder.getGetoffaddress();
        request.endLng = Double.parseDouble(rcOrder.getGetoffpositionlng());
        request.endLat = Double.parseDouble(rcOrder.getGetoffpositionlat());
        request.flightNum = rcOrder.getFlightnumber();
        DayPriceResponse dayPrice = rentCar.getDayPrice(request);
        if (dayPrice.IsSucess) {
            for (productDTO productDTO : dayPrice.queryResultList) {
                if (rcOrder.getCarlevel().toString().equals(String.valueOf(productDTO.carType))) {
                    unitPriceList.add(BigDecimal.valueOf(productDTO.price));
                    if (token != null)
                        rcOrder.setEstimateprice(BigDecimal.valueOf(productDTO.price));
                    else
                        rcOrder.setEstimateprice(BigDecimal.valueOf(productDTO.price).add(new BigDecimal(30)));
                    rcOrder.setAmontfrozen(BigDecimal.valueOf(productDTO.price).multiply(new BigDecimal(2)));
                    rcOrder.setActualprice(BigDecimal.valueOf(productDTO.price));
                    rcOrder.setPricemark(dayPrice.priceMark);
                }
            }
        } else {
            throw new Exception(dayPrice.msg);
        }
        calculationVOApi.setPrices(unitPriceList);//商品单价集合
        calculationVOApi.setAmount(1);//商品数量
        calculationVOApi.setAdditionalPrices(null);//附加商品单价集合
        calculationVOApi.setAdditionalAmount(0);//附加商品数量
        calculationVOApi.setCouponId(Long.valueOf(rcOrder.getCouponnum()));//优惠卷id
        calculationVOApi.setDepositAmount(depositSkipPwdAmount);//用户拥有的金币数量
        if (rcOrder.getIsdeposit().equals(0))
            calculationVOApi.setUseDeposit(false);//是否使用金币
        else
            calculationVOApi.setUseDeposit(true);//是否使用金币
        if (pntValue != null) {
            calculationVOApi.setPointAmount(Integer.parseInt(pntValue.toString()));//用户拥有的积分数量
        } else {
            calculationVOApi.setPointAmount(0);
        }
        if (rcOrder.getIsPoints().equals(0))
            calculationVOApi.setUsePoint(false);//是否使用积分
        else
            calculationVOApi.setUsePoint(true);//是否使用积分
        CalculationDTO calculationDTO = remoteCustomerService.calculationRemotely(calculationVOApi);
        BigDecimal couponAmount = calculationDTO.getPrice();

        BigDecimal payamount = new BigDecimal(rcOrder.getPaymentamount().toString());
        if (couponAmount.compareTo(payamount) != 0){
            throw new Exception("支付价格出现变动");
        }
        SnowFlake snowFlake = new SnowFlake(0, 0);
        rcOrder.setOrderid(String.valueOf(snowFlake.nextId()));
        rcOrder.setOrderstatus(1L);
        rcOrder.setPaymentstatus(10L);
        Date date = new Date();
        rcOrder.setCreateTime(date);
        rcOrder.setUpdateTime(date);
        rcOrder.setCartypeid(String.valueOf(rcOrder.getCarlevel()));
        rcOrder.setPassengernumbers(0L);
        rcOrder.setCreateorderperson(customer.getData().getCustomerName());
        rcOrder.setCreateorderpersontel(customer.getData().getContractPhoneOne());
        Map<String, String> map = carType(rcOrder.getCarlevel());
        rcOrder.setBrand(map.get("brand"));
        rcOrder.setCartypename(map.get("cartypename"));

        //TODO 测试需要，暂写死0.10元
        rcOrder.setPaymentamount(new BigDecimal(0.10));

        int order = this.insertRcOrder(rcOrder);
        rcOrderDetail.setRcorderid(rcOrder.getRcorderid());
        int detail = rcOrderDetailService.insertRcOrderDetail(rcOrderDetail);
        if ((pntValue != null && pntValue.longValue() > 0L) || Long.valueOf(rcOrder.getCouponnum()) != null || (longR.getData() != null && longR.getData().longValue() > 0L)) {
            BigDecimal decimal = new BigDecimal(0);
            if (longR.getData() != null){
                decimal = new BigDecimal(longR.getData());
            }
            remoteCustomerService.spend(customer.getData().getCustomerId(), pntValue, orderType, Long.valueOf(rcOrder.getOrderid()), null, Long.valueOf(rcOrder.getCouponnum()), decimal, 1L);
        }
        if (order == 1 && detail == 1) {
            redisService.setCacheObject("rentCar" + rcOrder.getJsjmemberid() + "o" + rcOrder.getOrderid(), JSON.toJSON(rcOrder), 60 * 30L, TimeUnit.SECONDS);
            RcOrderTracking orderTracking = new RcOrderTracking();
            orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
            orderTracking.setTrackingnode("提交订单");
            String serviceType = "";
            switch (rcOrder.getUsedcarway().toString()) {
                case "1":
                    serviceType = "接机";
                    break;
                case "2":
                    serviceType = "送机";
                    break;
                case "3":
                    serviceType = "接站";
                    break;
                case "4":
                    serviceType = "送站";
                    break;
            }
            orderTracking.setTrackingcontent("您的" + serviceType + "订单已提交，订单号：" + rcOrder.getOrderid() + "，应付金额：" + rcOrder.getPaymentamount() + "元，请在30分钟内完成支付。如需帮助可致电010-56939666。");
            orderTracking.setOpemployeeid(1L);
            orderTracking.setCreateTime(new Date());
            orderTracking.setUpdateTime(new Date());
            orderTracking.setRemark("系统消息");
            rcOrderTrackingService.insertRcOrderTracking(orderTracking);
            return String.valueOf(rcOrder.getOrderid());
        } else {
            return null;
        }
    }

    //订单收银台
    @Override
    public Cashier cashier(String token, String orderId) {
        Cashier cashier = new Cashier();
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        Long customerId = customer.getData().getCustomerId();
        Long expireTime = redisService.redisTemplate.getExpire("rentCar" + customerId + "o" + orderId);
        RcOrder rcOrder = rcOrderMapper.queryRcOrderByOrderid(Long.valueOf(orderId));
        BeanUtils.copyProperties(rcOrder, cashier);
        if (expireTime != null && expireTime > 0) {
            List<PayTypeApi> payTypes = payService.queryPayType("rentCar");
            for (PayTypeApi payType : payTypes) {
                PayTypeVo typeVo = new PayTypeVo();
                typeVo.setPayId(payType.getPayId());
                typeVo.setPayName(payType.getPayName());
                typeVo.setPayType(payType.getPayType());
                typeVo.setPayInfo(payType.getPayInfo());
                cashier.getPayType().add(typeVo);
            }
            cashier.setExpiredTime(String.valueOf(expireTime));
        } else {
            cashier.setExpiredTime("订单已超时！");
        }
        return cashier;
    }

    //专车订单列表
    @Override
    public List<RcOrderList> rcOrderList(String token, Long orderstatus) throws Exception {
        List<RcOrderList> rcOrderLists = new ArrayList<>();
        RcOrder rcOrder = new RcOrder();
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        if (customer.getCode() == 200 && customer.getData() != null) {
            rcOrder.setJsjmemberid(String.valueOf(customer.getData().getCustomerId()));
        } else {
            throw new Exception(customer.getMsg());
        }
        List<RcOrder> rcOrders = this.selectRcOrderList(rcOrder);
        rcOrders.forEach(item -> {
            RcOrderList list = new RcOrderList();
            if (orderstatus == 0) {
                if (item.getOrderstatus() == 1 || item.getOrderstatus() == 2 || item.getOrderstatus() == 3 || item.getOrderstatus() == 4 || item.getOrderstatus() == 8) {
                    String serviceType = carServiceType(item.getUsedcarway());
                    list.setServiceType(serviceType);
                    list.setOrderid(String.valueOf(item.getOrderid()));
                    BeanUtils.copyProperties(item, list);
                    rcOrderLists.add(list);
                }
            } else {
                if (item.getOrderstatus() == 5 || item.getOrderstatus() == 6 || item.getOrderstatus() == 7) {
                    String serviceType = carServiceType(item.getUsedcarway());
                    list.setServiceType(serviceType);
                    list.setOrderid(String.valueOf(item.getOrderid()));
                    BeanUtils.copyProperties(item, list);
                    rcOrderLists.add(list);
                }
            }
        });
        return rcOrderLists;
    }

    //订单详情-用车信息
    @Override
    public RcOrderCarInformation carInformation(String orderid) {
        RcOrderCarInformation information = new RcOrderCarInformation();
        RcOrder rcOrder = rcOrderMapper.queryRcOrderByOrderid(Long.valueOf(orderid));
        BeanUtils.copyProperties(rcOrder, information);
        information.setOrderid(String.valueOf(rcOrder.getOrderid()));
        information.setServiceType(carServiceType(rcOrder.getUsedcarway()));
        RcOrderPaymentrecord rcOrderPaymentrecord = rcOrderPaymentrecordService.selectRcOrderPaymentrecordByOrderId(Long.valueOf(orderid));
        if (rcOrderPaymentrecord != null) {
            information.setPayTime(rcOrderPaymentrecord.getCreattime());
            //information.setPaymentType();
        }
        return information;
    }

    //专车银行卡支付
    @Override
    public String bankCardPay(RcOrderBankCardPay bankCardPay, HttpServletRequest servletRequest) throws Exception {
        String token = servletRequest.getHeader("token");
        Employee employee = remoteCustomerService.getLoginEmployeeByToken(token).getData();
        if (employee != null){
            throw new Exception("登录已过期");
        }

        RcOrder rcOrder = rcOrderMapper.queryRcOrderByOrderid(Long.valueOf(bankCardPay.jsjOrderId));
        if (bankCardPay.amount != null && bankCardPay.amount.compareTo(new BigDecimal(0)) > 0){
            bankCardPay.moneyAmout = String.valueOf(new BigDecimal(bankCardPay.moneyAmout).subtract(bankCardPay.amount));

            rcOrder.setDepositamount(bankCardPay.amount);
            int i = rcOrderMapper.updateRcOrder(rcOrder);
            if (i <= 0){
                throw new Exception("修改订单预留款失败");
            }

            DepositBusinessApi depositBusiness = new DepositBusinessApi();
            depositBusiness.setRemark("专车银行卡支付");
            depositBusiness.setState("false");
            depositBusiness.setBusinessType(EmHelper.businessType.SpecialCar.getValue());
            depositBusiness.setOperatorId(employee.getEmployeeId());
            depositBusiness.setOrdersourceid(bankCardPay.payType);
            depositBusiness.setCustomerId(Long.valueOf(rcOrder.getJsjmemberid()));
            depositBusiness.setDetailamount(bankCardPay.amount);
            depositBusiness.setChangetype(2l);
            depositBusiness.setLockState("false");
            depositBusiness.setOrderId(Long.valueOf(rcOrder.getOrderid()));
            depositBusiness.setDrawState("false");
            Long aLong = remoteCustomerService.addReserveMoney(depositBusiness);
            if (aLong <= 0){
                throw new Exception("添加预留款记录失败");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("id", aLong);
            map.put("remark", "专车银行卡支付");
            boolean b = remoteCustomerService.manageAudit(map);
            if (!b){
                throw new Exception("审核失败，请进行人工审核");
            }
        }
        CustomerApi customer = new CustomerApi();
        customer.setCustCertificateNo(bankCardPay.custCertificateNo);
        customer.setContractPhoneOne(bankCardPay.contractPhoneOne);
        //银行卡信息
        BankCardApi card = new BankCardApi();
        card.setCardNumber(bankCardPay.cardNumber);
        card.setCardholder(bankCardPay.cardholder);

        Map<String, Object> map = new HashMap<>();
        map.put("cardNumber", bankCardPay.cardNumber);
        String backCard = remoteCustomerService.backCard(map);

        card.bankCode = backCard + "CREDIT";
        card.setCreditCardTime(bankCardPay.creditCardTime);
        card.setCvv2(bankCardPay.Cvv2);

        BankPayInfoApi bankPayInfoApi = new BankPayInfoApi();
        bankPayInfoApi.setJsjOrderId(bankCardPay.jsjOrderId);
        bankPayInfoApi.setMoneyAmout(String.valueOf(rcOrder.getPaymentamount()));
        bankPayInfoApi.setCustomer(customer);
        bankPayInfoApi.setCard(card);
        if (rcOrder.getOrdersource() == 3)
            bankPayInfoApi.setPayType("Crm");
        else
            bankPayInfoApi.setPayType("App");


        YeeBaoResultApi yeeBaoResultApi;
        if (bankCardPay.type.equals("creditcard")){
            yeeBaoResultApi = payService.CreditSale(bankPayInfoApi);
        }
        else if (bankCardPay.type.equals("debitcard")){
            yeeBaoResultApi = payService.DebitSale(bankPayInfoApi);
        }
        else {
            throw new Exception("支付类型不明确");
        }

        System.out.println("订单号：" + bankCardPay.jsjOrderId);
        System.out.println("交易流水号：" + yeeBaoResultApi.getTrxId());
        RcOrderPaymentrecord paymentrecord = new RcOrderPaymentrecord();
        RcOrderTracking orderTracking = new RcOrderTracking();
        if (yeeBaoResultApi.getCode().equals("1")){
            paymentrecord.setOrderid(Long.valueOf(rcOrder.getOrderid()));
            paymentrecord.setBankcardnumber(DigestUtils.md5Hex(bankCardPay.cardNumber).toUpperCase());
            paymentrecord.setBankcardholdername(bankCardPay.cardholder);
            paymentrecord.setTransactionnumber(yeeBaoResultApi.getTrxId());
            paymentrecord.setPaymenttype(2L);
            paymentrecord.setPaymentstatus(31L);
            paymentrecord.setCreattime(new Date());
            paymentrecord.setUpdateTime(new Date());
            paymentrecord.setRemark("租车订单-订单号" + rcOrder.getOrderid() + "，支付金额-" + rcOrder.getPaymentamount());
            rcOrderPaymentrecordService.insertRcOrderPaymentrecord(paymentrecord);

            orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
            orderTracking.setTrackingnode("付款成功");
            orderTracking.setTrackingcontent("您已支付成功，本次支付金额" + rcOrder.getPaymentamount() + "元。");
            orderTracking.setOpemployeeid(1L);
            orderTracking.setCreateTime(new Date());
            orderTracking.setUpdateTime(new Date());
            orderTracking.setRemark("系统消息");
            rcOrderTrackingService.insertRcOrderTracking(orderTracking);
            rcOrder.setPaymentstatus(31L);
            rcOrderMapper.updateRcOrder(rcOrder);
            if (bankCardPay.jsjOrderId != null) {
                CreateOrderRequest request = new CreateOrderRequest();
                request.priceMark = rcOrder.getPricemark();
                switch (rcOrder.getUsedcarway().toString()) {
                    case "1":
                        request.serviceType = 11;
                        break;
                    case "2":
                        request.serviceType = 12;
                        break;
                    case "3":
                        request.serviceType = 21;
                        break;
                    case "4":
                        request.serviceType = 22;
                        break;
                }
                request.carType = Math.toIntExact(rcOrder.getCarlevel());
                request.mobile = rcOrder.getPassengertel();
                request.flightNumber = rcOrder.getFlightnumber();
                request.name = rcOrder.getPassengername();
                request.vendorOrderCode = String.valueOf(rcOrder.getOrderid());
                request.promotionInfo = "";
                CreateOrderResponse order = rentCar.createOrder(request);
                if (order.IsSucess) {
                    orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                    orderTracking.setTrackingnode("派单中");
                    orderTracking.setTrackingcontent("您的订单正在派单，请等待司机接单。");
                    orderTracking.setOpemployeeid(1L);
                    orderTracking.setCreateTime(new Date());
                    orderTracking.setUpdateTime(new Date());
                    orderTracking.setRemark("系统消息");
                    rcOrderTrackingService.insertRcOrderTracking(orderTracking);
                    rcOrder.setBussinessorderid(order.orderCode);
                    rcOrder.setOrderstatus(2L);
                    rcOrderMapper.updateRcOrder(rcOrder);
                } else {
                    orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                    orderTracking.setTrackingnode("订单异常");
                    orderTracking.setTrackingcontent(order.ErrorMessage + "/" + order.msg + "。");
                    orderTracking.setOpemployeeid(1L);
                    orderTracking.setCreateTime(new Date());
                    orderTracking.setUpdateTime(new Date());
                    orderTracking.setRemark("系统消息");
                    rcOrderTrackingService.insertRcOrderTracking(orderTracking);
                    rcOrder.setBussinessorderid(order.orderCode);
                    rcOrder.setOrderstatus(7L);
                    rcOrderMapper.updateRcOrder(rcOrder);
                }
            }
            return "支付成功";
        }
        else {
            rcOrder.setPaymentstatus(50L);
            rcOrderMapper.updateRcOrder(rcOrder);
            RcOrderPaymentrecord rcOrderPaymentrecord = rcOrderPaymentrecordService.selectRcOrderPaymentrecordByOrderId(Long.valueOf(rcOrder.getOrderid()));
            if (rcOrderPaymentrecord==null)
            {
                paymentrecord.setOrderid(Long.valueOf(rcOrder.getOrderid()));
                paymentrecord.setBankcardnumber(DigestUtils.md5Hex(bankCardPay.cardNumber).toUpperCase());
                paymentrecord.setBankcardholdername(bankCardPay.cardholder);
                paymentrecord.setTransactionnumber(yeeBaoResultApi.getTrxId());
                paymentrecord.setPaymenttype(2L);
                paymentrecord.setPaymentstatus(50L);
                paymentrecord.setCreattime(new Date());
                paymentrecord.setUpdateTime(new Date());
                paymentrecord.setRemark("租车订单-订单号" + rcOrder.getOrderid() + "，支付金额-" + rcOrder.getPaymentamount());
                rcOrderPaymentrecordService.insertRcOrderPaymentrecord(paymentrecord);
            }
            orderTracking.setOrderid(Long.valueOf(rcOrder.getOrderid()));
            orderTracking.setTrackingnode("支付异常");
            orderTracking.setTrackingcontent("您的订单支付失败。");
            orderTracking.setOpemployeeid(1L);
            orderTracking.setCreateTime(new Date());
            orderTracking.setUpdateTime(new Date());
            orderTracking.setRemark("系统消息");
            rcOrderTrackingService.insertRcOrderTracking(orderTracking);
            return yeeBaoResultApi.getErrorMsg();
        }
    }

    //专车订单详情
    @Override
    public RcOrderVO rcOrderDetail(String orderid) {
        RcOrderVO rcOrderVO = new RcOrderVO();
        RcOrder rcOrder = rcOrderMapper.rcOrderDetail(orderid);
        RcOrderDetail rcOrderDetail = rcOrderDetailService.selectRcOrderDetailById(rcOrder.getRcorderid());
        BeanUtils.copyProperties(rcOrderDetail, rcOrderVO);
        BeanUtils.copyProperties(rcOrder, rcOrderVO);
        RcOrderTracking tracking = new RcOrderTracking();
        tracking.setOrderid(Long.valueOf(orderid));
        List<RcOrderTracking> rcOrderTrackings = rcOrderTrackingService.selectRcOrderTrackingList(tracking);
        if (rcOrderTrackings != null && rcOrderTrackings.size() > 0)
            rcOrderVO.getRcOrderTrackings().addAll(rcOrderTrackings);
        return rcOrderVO;
    }

    private Map<String, String> carType(Long carLevel) {
        Map<String, String> map = new HashMap<>();
        switch (carLevel.toString()) {
            case "1":
                map.put("brand", "大众宝来、别克凯越等同级车");
                map.put("cartypename", "经济");
                break;
            case "2":
                map.put("brand", "大众帕萨特、本田雅阁等同级车");
                map.put("cartypename", "舒适");
                break;
            case "3":
                map.put("brand", "别克GL8、本田奥德赛等同级车");
                map.put("cartypename", "商务");
                break;
            case "4":
                map.put("brand", "奥迪A6L等同级车");
                map.put("cartypename", "豪华");
                break;
            case "5":
                map.put("brand", "宝马7系、奔驰S350等同级车");
                map.put("cartypename", "奢华");
                break;
            case "14":
                map.put("brand", "丰田海狮、全顺面包、奔驰MB100等同级车");
                map.put("cartypename", "10座小巴");
                break;
            case "15":
                map.put("brand", "小金龙、考斯特等同级车");
                map.put("cartypename", "20座中巴");
                break;
            case "16":
                map.put("brand", "金龙中巴、宇通中巴等同级车");
                map.put("cartypename", "35座大巴");
                break;
            case "17":
                map.put("brand", "金龙大巴、宇通大巴等同级车");
                map.put("cartypename", "45座大巴");
                break;
            case "18":
                map.put("brand", "丰田海狮等同级车");
                map.put("cartypename", "8座小巴");
                break;
            case "19":
                map.put("brand", "宇通、金龙等同级车");
                map.put("cartypename", "15座中巴");
                break;
            case "20":
                map.put("brand", "金龙等同级车");
                map.put("cartypename", "25座中巴");
                break;
            case "21":
                map.put("brand", "金龙等同级车");
                map.put("cartypename", "30座大巴");
                break;
            case "22":
                map.put("brand", "金龙等同级车");
                map.put("cartypename", "40座大巴");
                break;
            case "23":
                map.put("brand", "大金龙、宇通等同级车");
                map.put("cartypename", "50座大巴");
                break;
            case "24":
                map.put("brand", "金龙、宇通等同级车");
                map.put("cartypename", "53座大巴");
                break;
        }
        return map;
    }

    private String carServiceType(Long usedcarway) {
        String serviceType = null;
        switch (usedcarway.toString()) {
            case "1":
                serviceType = "接机";
                break;
            case "2":
                serviceType = "送机";
                break;
            case "3":
                serviceType = "接站";
                break;
            case "4":
                serviceType = "送站";
                break;
            case "5":
                serviceType = "日租";
                break;
            case "6":
                serviceType = "半日租";
                break;
            case "7":
                serviceType = "时租";
                break;
            case "8":
                serviceType = "随叫随到";
                break;
        }
        return serviceType;
    }
}
